<template>
  <div class="wrapper">
    <div style="margin:100px auto;background-color: #fff;width: 400px;height: 450px;padding: 20px;border-radius: 10px">
      <div style="margin:20px 0;text-align: center;font-size: 24px"><b>注册</b></div>
        <el-form :model="user" status-icon :rules="rules"  ref="regForm">
          <el-form-item prop="userName">
            <el-input placeholder="请输入账号" size="medium" style="margin:5px 0" prefix-icon="el-icon-user" v-model="user.userName"></el-input>
          </el-form-item>
          <el-form-item  prop="password">
            <el-input placeholder="请输入密码" size="medium" style="margin:5px 0" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item  prop="confirmPassword">
            <el-input placeholder="请确认密码" size="medium" style="margin:5px 0" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
          </el-form-item>
          <el-form-item  prop="realName">
            <el-input placeholder="昵称" size="medium" style="margin:5px 0" prefix-icon="el-icon-user" v-model="user.realName"></el-input>
          </el-form-item>
          <el-form-item style="margin: 5px 0;text-align: right">
            <el-button type="primary" size="small" autocomplete="off" @click="Register">注册</el-button>
            <el-button type="warning" size="small" autocomplete="off" @click="$router.push('/')">返回登录</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>


</template>

<style>
.wrapper{
  display: flex;
  height:100vh;
  background-image: linear-gradient(to bottom right,#FC4668,#3F5EFB);
  overflow: hidden;
}

</style>

<script>
export default {
  name: "Register",
  data(){
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.user.confirmPassword !== '') {
          this.$refs.regForm.validateField('confirmPassword');
        }
        callback();
      }
    };
    var validateConfirmPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return{
      user:{
        userName:'',
        password:'',
        confirmPassword: '',
        logoUrl:'null',
        realName:''
      },
      rules: {
         userName:[
           { required:true, message: '请输入用户名', trigger:'blur'},
           { min:3, max:10 ,message: '用户名长度为3-10个字符！', trigger:'blur'}
         ],
        password:[
           { required:true, message:'请输入密码', trigger:'blur'},
           { min:8, max:20 ,message: '请输入8-20个字符', trigger: 'blur'},
           { validator: validatePass,trigger: 'blur'}
        ],
        confirmPassword:[
          { validator:validateConfirmPass,trigger:'blur'}
        ],
        realName: [
          { required:true, message:'请输入昵称', trigger:'blur' }
        ]
      }
    };
  },
  methods:{
    Register(){
      this.$refs['regForm'].validate((valid)=>{
        if(valid){
          console.log(this.user)
          this.request.post("/user/register",this.user).then(res => {
            if(res.code=='200'){
              this.$message.success('注册成功！')
              setTimeout(()=>{this.$router.push('/')},1000)
            }
            else this.$message.error(res.msg)
          })
        }
      })
    },
  }
}
</script>

